const db = require('../../MySql');
/**
  * 构造函数 - 创建考试题库关联对象实例
  * 用于封装考试与题库之间的关联关系，以及各类题型的数量和分值配置
  *
*/
class el_exam_repo {
    /**
     * @param {string} id - 关联记录ID，唯一标识符
     * @param {string} exam_id - 考试ID，关联的考试规则
     * @param {string} repo_id - 题库ID，关联的题库
     * @param {number} radio_count - 单选题数量，考试中单选题的总数
     * @param {number} radio_score - 单选题分值，每道单选题的分数
     * @param {number} multi_count - 多选题数量，考试中多选题的总数
     * @param {number} multi_score - 多选题分值，每道多选题的分数
     * @param {number} judge_count - 判断题数量，考试中判断题的总数
     * @param {number} judge_score - 判断题分值，每道判断题的分数
     * @param {number} saq_count - 简答题数量，考试中简答题的总数
     * @param {number} saq_score - 简答题分值，每道简答题的分数
     * 
     * 使用示例：
     * const examRepo = new el_exam_repo(
     *     'er001', 'exam123', 'repo456',
     *     20, 2,   // 20道单选题，每题2分
     *     10, 3,   // 10道多选题，每题3分
     *     5, 1,    // 5道判断题，每题1分
     *     2, 10    // 2道简答题，每题10分
     * );
     * 
     * 总分计算示例：
     * 总分 = (radio_count * radio_score) + (multi_count * multi_score) + 
     *       (judge_count * judge_score) + (saq_count * saq_score)
     * 总分 = (20*2) + (10*3) + (5*1) + (2*10) = 40 + 30 + 5 + 20 = 95分
     */
    constructor(id, exam_id, repo_id, radio_count, radio_score, multi_count, multi_score, judge_count, judge_score, saq_count, saq_score) {
        this.id = id
        this.exam_id = exam_id
        this.repo_id = repo_id
        this.radio_count = radio_count
        this.radio_score = radio_score
        this.multi_count = multi_count
        this.multi_score = multi_score
        this.judge_count = judge_count
        this.judge_score = judge_score
        this.saq_count = saq_count
        this.saq_score = saq_score
    }

    //根据考试ID查询考试题库关联记录
    /**
     * @param {string} exam_id - 考试ID，用于查询关联的题库记录
     * @returns {Promise<Array>} - 返回包含所有符合条件的考试题库关联记录的数组
     */
    static async findExamRepoByExamId(exam_id){
        const sql = `select * from el_exam_repo where exam_id=?`
        const params = [exam_id]
        const [results] = await db.query(sql,params)
        return results
    }

    //统计每个题库对应的单选题、多选题、判断题数量
    /**
     * @param {string} repo_id - 题库ID，用于查询关联的题库记录
     * @returns {Promise<Array>} - 返回包含每个题库对应的单选题、多选题、判断题数量的数组
     */
    static async countQuTypesByRepoId(repo_id){
        const sql = `
                SELECT 
                    SUM(CASE WHEN qu_type=2 THEN 1 ELSE 0 END) as multiCount,
                    SUM(CASE WHEN qu_type=1 THEN 1 ELSE 0 END) as radioCount,
                    SUM(CASE WHEN qu_type=3 THEN 1 ELSE 0 END) as judgeCount
                FROM el_qu_repo 
                WHERE repo_id=?`
        const params = [repo_id]
        const [results] = await db.query(sql,params)
        return results
    }



}


module.exports = el_exam_repo